package cn.com.gridinfo.sooc.core.expand.callname.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 添加学生
 *
 * @author yingchunli
 */
public class AddStudentActivity extends BaseSoocActivity {


    @InjectView(R.id.name_addstudent)
    ClearEditText name;//姓名
    @InjectView(R.id.number_addstudent)
    ClearEditText number;//学号
    @InjectView(R.id.class_addstudent)
    ClearEditText clzss;//班级
    @InjectView(R.id.commit_addstudent)
    Button commitBtn;//提交按钮

    CallNameDao dao;
    @InjectView(R.id.url_load)
    TextView urlLoad;//上传学生的地址url

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_callname_addstudent);
        ButterKnife.inject(this);
        dao = new CallNameDao(this);
        commitBtn.setEnabled(false);

        urlLoad.setText("添加名单太麻烦？\n请登录" + Arad.preferences.getString(LocalMark.SERVER_URL.getName()) + ", 可以通过Excel上传名单。");

        //按键完成监听
        clzss.setImeOptions(EditorInfo.IME_ACTION_DONE);
        clzss.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    if (validateTiYanNotNull()) {
                        onClick();
                    }
                }
                return false;
            }
        });
    }

    /**
     * 判断必填内容是否为空，确定注册按钮处于什么状态
     * 当必填内容全不为空时，提交 按钮才处于可点击状态，否则，不可点击
     */
    @OnTextChanged(value = {R.id.name_addstudent, R.id.number_addstudent, R.id.class_addstudent},
            callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterTextChanged() {
        if (name.getText().toString().trim().equals("") || number.getText().toString().trim().equals("") || clzss.getText().toString().trim().equals("")) {
            commitBtn.setEnabled(false);
        } else {
            commitBtn.setEnabled(true);
        }
    }

    /**
     * 提交按钮的点击
     */
    @OnClick(R.id.commit_addstudent)
    void onClick() {
        //提交
        String nameStr = name.getText().toString().trim();
        String numberStr = number.getText().toString().trim();
        String clzssStr = clzss.getText().toString().trim();
        Log.d("姓名name->" + nameStr);
        Log.d("学号number->" + numberStr);
        Log.d("班级clzss->" + clzssStr);
        //判断信息是否符合要求
        if (judge()) {
            dao.addStudent(getIntent().getStringExtra("kcid"), nameStr, numberStr, clzssStr, CodeConst.EXPAND_ADD_STUDENT);
        }
    }

    /**
     * 判断数据是否填写正确
     */
    private boolean judge() {
        if (StringUtils.isNull(name.getText().toString().trim())) {
            MessageUtils.showImageShortToast(this, "学生姓名不能为空");
            return false;
        } else if (StringUtils.isNull(number.getText().toString().trim())) {
            MessageUtils.showImageShortToast(this, "学生学号不能为空");
            return false;
        } else if (StringUtils.isNull(clzss.getText().toString().trim())) {
            MessageUtils.showImageShortToast(this, "学生班级不能为空");
            return false;
        } else {
            return true;
        }
    }

    /**
     * 验证信息是否全部填写
     *
     * @return
     */
    boolean validateTiYanNotNull() {
        if (StringUtils.isNull(name.getText().toString().trim())) {
            name.requestFocus();
            return false;
        } else if (StringUtils.isNull(number.getText().toString().trim())) {
            number.requestFocus();
            return false;
        } else if (StringUtils.isNull(clzss.getText().toString().trim())) {
            clzss.requestFocus();
            return false;
        }
        return true;
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        switch (requestCode) {
            case CodeConst.EXPAND_ADD_STUDENT:
                //添加学生请求成功
                if (dao.getStatus().equals("0")) {
                    MessageUtils.showRightImageShortToast(this, "添加学生成功");
                    setResult(RESULT_OK);
                    finish();
                } else {
                    MessageUtils.showImageShortToast(this, dao.getStatusCode());
                }
                break;
        }
    }

    @Override
    public String setupToolBarTitle() {
        return "添加学生";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftclick() {
        finish();
    }
}
